package com.bobo.web.provider.util.commutils;

import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TextUtils {
    private static Logger logger = LoggerFactory.getLogger(TextUtils.class.getSimpleName());

    public static final String SEPERATOR = " - ";
    public static final String SPACE = " ";
    public static final String TAB = "\t";
    public static final String LINE = "\n";
    public static final String NONE = "";
    public static final String COLON = ":";
    public static final String SEMICOLON = ";";
    public static final String COMMA = ",";
    public static final String DOT = ".";
    public static final String LFET_SLASH = "/";
    public static final String RIGHT_SLASH = "\\";

    public static final Pattern digit = Pattern.compile("[0-9]+");
    public static final Pattern engStr = Pattern.compile("[a-zA-Z ]+");
    public static final Pattern chiStr = Pattern.compile("[0-9a-zA-Z\u4e00-\u9fa5 ]+");

    public static boolean isNumber(String str){
        return getStringType(str).equals(StringTypeEnum.digit);
    }

    public static boolean isEngStr(String str){
        return getStringType(str).equals(StringTypeEnum.engStr);
    }

    public static boolean isChiStr(String str){
        return getStringType(str).equals(StringTypeEnum.chiStr);
    }

    public static StringTypeEnum getStringType(String str){

        Matcher mt1 = digit.matcher(str);
        if(mt1.matches()){
            return StringTypeEnum.digit;
        }


        Matcher mt2 = engStr.matcher(str);
        if(mt2.matches()){
            return StringTypeEnum.engStr;

        }


        Matcher mt3 = chiStr.matcher(str);
        if(mt3.matches()){
            return StringTypeEnum.chiStr;

        }

        return StringTypeEnum.none;

    }

    public enum StringTypeEnum{
        digit,engStr,chiStr,none
    }

    public static boolean isEmpty(String string) {
        if (string == null || string.equals(NONE)) {
            return true;
        } else {
            return false;
        }
    }

    public static String generateRandomNumber(int n){
        Random rd = new Random();
        boolean[] isExists = new boolean[10];
        StringBuilder sb = new StringBuilder();
        int randomNumber = 0;
        for (int i=0; i<n; i++){
            do{
                randomNumber = rd.nextInt(10);
            }while(isExists[randomNumber]);
            isExists[randomNumber] = true;
            sb.append(randomNumber);
        }
        return sb.toString();
    }

    public static String removeEnter(String string){
        if(string != null) {
            return string.replace(TextUtils.LINE, TextUtils.NONE);
        }else {
            return null;
        }
    }

    public static String obscureMobileNumber(String phone){
        if (StringUtils.isNotEmpty(phone)){
            if (phone.contains("*")){
                return phone;
            }
            Pattern pattern = Pattern.compile("^[0-9]\\d{10}$");
            Matcher matcher = pattern.matcher(phone);
            boolean bl = matcher.matches();
            if (phone.length()>6){
                if (bl) {
                    return phone.substring(0, 3) + "****" + phone.substring(7);
                } else {
                    return phone.substring(0, phone.length() - 4) + "****";
                }
            }
            return "*****";
        }else {
            return null;
        }
    }

    /**
     * 根据中英文名称生成UI展现的studentName
     * @param name
     * @param englishName
     * @return
     */
    public static String genStudentName(String name ,String englishName){
        if(StringUtils.isEmpty(name)){
            name ="";
        }
        if(StringUtils.isEmpty(englishName)){
            englishName = "";
        }
        return name + " - " + englishName;
    }

    /**
     * 根据分割字符切分字符串获得id列表
     * @param idsStr
     * @param seperator
     * @return
     */
    public static List<Long> parseIds(String idsStr, String seperator){
        if(StringUtils.isEmpty(idsStr) || StringUtils.isEmpty(seperator)){
            return Collections.EMPTY_LIST;
        }
        String [] idAry = idsStr.split(seperator);
        List<Long> ids = Lists.newArrayListWithCapacity(idAry.length);
        for (int i = 0 ;i < idAry.length;i++){
            Long id = -1L;
            try{
                id = Long.parseLong(idAry[i]);
            }catch (NumberFormatException e){
                logger.error(" invalid format str: " +idAry[i] );
            }
            if(id > 0 && !ids.contains(id)){ ids.add(id); }
        }
        return ids;
    }

    public static boolean isMobileByWeakJudge(String text){
        Pattern p = Pattern.compile("^[1][0-9][0-9]{9}$");
        Matcher m = p.matcher(text);
        boolean b = m.matches();
        return b;
    }
}
